﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMScore_Subsets
{
    public partial class MainForm : Form //main form
    {
        private List<User> usersList; // all the users
        public MainForm()
        {
            InitializeComponent();
            usersList = new List<User>();
        }


        /// <summary>
        /// Initializes all program
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> m_AllDocs1=new Dictionary<string,string>();
            button1.Enabled=false;
            Tuple<int, int, bool, bool, Dictionary<string, Tuple<string, List<string>, DateTime, double>>, int, int> allDoc = ReadFile.ReadAllFile();
            foreach(string name in allDoc.Item5.Keys)
            {
                m_AllDocs1[name]=allDoc.Item5[name].Item1;

            }
            Dictionary<string, string> m_AllDocs = Parser.MainFunction( allDoc.Item3,m_AllDocs1);
            DocumentsManager manager = new DocumentsManager();

            usersList = manager.CreateAllDocuments(allDoc, m_AllDocs);
            button1.Enabled = true;
            foreach(User u in usersList)
            {
                listBox1.Items.Add(u.m_name);
                
           
            }

        }
        /// <summary>
        /// write into file all the users and their TMscore
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            UsersManager.WritetoFile(usersList);
            UsersManager.CreateCSVFile(usersList);


        }
        /// <summary>
        /// choose a user and show his graph of TMscore
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            User user=new User();
                string name=listBox1.SelectedItem.ToString();
                foreach (User u in usersList)
                {
                    if (u.m_name == name)
                    {

                        user = u;
                    }
                }
            
              Form PresentingTMscore= new PresentingTMscore(user);
              PresentingTMscore.ShowDialog();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            foreach (User u in usersList)
            {
                u.CheckForAbnormal();
            }

            using (CsvFileWriter writer = new CsvFileWriter("users-slope.csv"))
            {


                foreach (User u in usersList)
                {
                    if (u.subsetTMscore.Count > 1)
                    {
                        CsvRow row = new CsvRow();
                        row.Add(String.Format("{0}", u.m_name));
                        foreach (double slope in u.slopes)
                        {
                            row.Add(String.Format("{0}", slope));
                        }
                        writer.WriteRow(row);
                    }

                }
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //foreach (User u in usersList)
            //{
            //    u.UserAlert();
            //}

        }
    }
}
